// pages/address/address.js
const app = getApp()
var host = app.globalData.host;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag: 0,
        addresses: [],
        goodsId: '',
        num: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        console.log(e)
        this.setData({ goodsId: e.goodsId })
        this.setData({ num: e.num })
        this.loadAddress()
    },
    newAddress: function(e) {
        wx.navigateTo({
            url: '../newAddress/newAddress?goodsId=' + this.data.goodsId + '&num=' + this.data.num
        })
    },
    editAddress: function(e) {
        wx.navigateTo({
            url: '../newAddress/newAddress?addressId=' + e.currentTarget.id + '&goodsId=' + this.data.goodsId + '&num=' + this.data.num
        })
    },
    loadAddress: function() {
        var page = this;
        var userId = wx.getStorageSync("userId");
        if (userId != "") {
            wx.request({
                url: host + '/api/address/selectAddressByUserId',
                method: 'GET',
                data: {
                    "userId": userId,
                    // "userId": 2766
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    var code = res.data.code;
                    var addresses = res.data.data;
                    if (code = '0000') {
                        page.setData({ addresses: addresses });
                    }
                    console.log(addresses)
                }
            })
        } else {
            wx.redirectTo({
                url: '../login/login'
            })
        }
    },
    switchNav: function(e) {
        var index = e.currentTarget.id;
        this.setData({ flag: index });
        //需要从别的页面上获取，在data中拿到的，看一下onLoad里面的e是否存在
        var addressId = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../buy/buy?addressId=' + addressId + '&goodsId=' + this.data.goodsId + '&num=' + this.data.num
        })
    },


})